//
//  RJWarningTable.m
//  AirQuality
//
//  Created by Po on 15/4/8.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJWarningTable.h"
#import "RJWarningDay.h"
#import "RJTools.h"
@interface RJWarningTable () <RJWarningFatherDeleaget, RJWarningDayDelegate>
@property (retain, nonatomic) NSMutableArray * daysArray;

- (void)initInterface;
@end

@implementation RJWarningTable

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setFatherDelegate:self];
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    [self getAllDayTable];
}

- (void)getAllDayTable
{
    _daysArray = [[NSMutableArray alloc] init];
    NSUInteger dayCount = 1;
    for (NSUInteger i = 0; i < 3 ; i ++) {
        RJWarningDay * day = [[RJWarningDay alloc] initWithFrame:CGRectMake(0, 220 *  i, ScreenWidth, 200)];
        [day setDelegate:self];
        [day.timeLabel setText:[RJTools getTimeWithFormat:@"MM月dd日" time:GetFutureSinceNow(dayCount)]];
        [self addSubview:day];
        [_daysArray addObject:day];
        dayCount ++;
    }
}

#pragma mark RJWarningFatherDelegate
- (void)WarningFatherSetData:(NSArray *)data
{
//    BOOL isAudit = [RJUserData defaultRJUserData].isAudit;
    BOOL isDone = [RJUserData defaultRJUserData].isDone;
    BOOL isHistory = [RJUserData defaultRJUserData].isHistory;
    
    NSArray * pm25data = data[0];
    for (NSUInteger i = 0; i < 3; i ++) {
        RJWarningDay * day = _daysArray[i];
        [day.hour48Value setText:pm25data[i][0]];
        [day.hour72Value setText:pm25data[i][1]];
        [day.hour96Value setText:pm25data[i][2]];
        if (isDone || isHistory) {
            //根据状态来改变表格数据
            day.textFiled.text = data[1][i];
            [day.concentrationLabel setText:data[2][i]];
            [day.AQILabel setText:data[3][i]];
        }
    }
}

#pragma mark RJWarningDayDelegate
- (void)WarningDay:(RJWarningDay *)warningDay number:(CGFloat)number
{
    if (number == 0) {
        [warningDay.concentrationLabel setText:@""];
        [warningDay.AQILabel setText:@""];
        return;
    }
    
    NSUInteger mathNum = [RJTools getPM25MathNum:number];
    NSNumber * pm25Max = @(number + mathNum);
    NSNumber * pm25Min = @(number - mathNum);
    pm25Min = [pm25Min integerValue] < 0 ? @0 : pm25Min;
    [warningDay.concentrationLabel setText:[NSString stringWithFormat:@"%@~%@",pm25Min, pm25Max]];
    NSNumber * aqiMax = @([RJTools getAQINumber:[pm25Max floatValue]]);
    NSNumber * aqiMin = @([RJTools getAQINumber:[pm25Min floatValue]]);
    [warningDay.AQILabel setText:[NSString stringWithFormat:@"%@~%@",aqiMin, aqiMax]];
}

- (void)WarningFatherSetUserData:(NSDictionary *)data
{
    if (!data) {
        return;
    }
    
    NSArray * pm25 = data[@"pm2_5"];
    NSArray * pm25DR = data[@"pm2_5_dr"];
    NSArray * pm25IR = data[@"pm2_5_ir"];
    
    for (NSUInteger i = 0; i < 3; i ++) {
        RJWarningDay * day = _daysArray[i];
        if (pm25 && pm25[i]) {
            day.textFiled.text = pm25[i];
        }
        if (pm25DR && pm25DR[i]) {
            day.concentrationLabel.text = pm25DR[i];
        }
        if (pm25IR && pm25IR[i]) {
            day.AQILabel.text = pm25IR[i];
        }
    }
}

- (void)WarningFatherWillRemoveSelf
{
    [self endEditing:YES];
}

- (NSDictionary *)WarningFatherGetData
{
    NSMutableArray * pm25 = [NSMutableArray array];
    NSMutableArray * pm25DR = [NSMutableArray array];
    NSMutableArray * pm25IR = [NSMutableArray array];
    for (NSUInteger i = 0; i < 3; i ++) {
        RJWarningDay * day = _daysArray[i];
        [pm25 addObject:day.textFiled.text];
        [pm25DR addObject:day.concentrationLabel.text];
        [pm25IR addObject:day.AQILabel.text];
    }
    return @{@"pm2_5":pm25,@"pm2_5_dr":pm25DR, @"pm2_5_ir":pm25IR};
}




@end
